package com.cz.modules.jk.service.impl;

import com.cz.common.utils.ParkingUtil;
import com.cz.modules.jk.service.ScreenPushService;
import nova.traffic.NovaTraffic;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("ScreenPushService")
public class ScreenPushServiceImpl  implements ScreenPushService {

    @Override
    public void pushsmallS(HttpServletRequest request){
        String fontSize = request.getParameter("fontSize");
        if("60".equals(fontSize)){
            fontSize = "24";
        }else{
            fontSize = "32";
        }
        String fontColor = request.getParameter("fontColor");
        String info = request.getParameter("info");
        try {
            String aa="<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.50\" Port=\"9999\"><PROG id=\"-1\"><ITEMS trad=\"0,0\" wh=\"160,128\"><ITEM type=\"2\" interval=\"65534\"><text color=\""+fontColor+"\" size=\""+fontSize+"\" style=\"0\"  font=\"宋体\">"+info+"</text>" +
                "</ITEM></ITEMS></PROG></ITS></QDELITS>";
            ParkingUtil.pw.write(aa);
            ParkingUtil.pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pushScreenS() {
        int p1=0,p2=0,p3=0,p4=0;//每区剩余车位数
        int p1u = 0,p2u=0,p3u=0,p4u=0;//每区使用车位数
        Map<String,Integer> map1 = ParkingUtil.southMap.get("超长停车区");
        if(map1 != null){
            for(Map.Entry<String,Integer> en:map1.entrySet()){
                p1u += en.getValue();
            }
        }
        p1 = ParkingUtil.southCountMap.get("超长停车区")-p1u;
        Map<String,Integer> map2 = ParkingUtil.southMap.get("前广场大车区");
        if(map2 != null) {
            for (Map.Entry<String, Integer> en : map2.entrySet()) {
                p2u += en.getValue();
            }
        }
        p2 = ParkingUtil.southCountMap.get("前广场大车区")-p2u;
        Map<String,Integer> map3 = ParkingUtil.southMap.get("小车区");
        if(map3 != null) {
            for (Map.Entry<String, Integer> en : map3.entrySet()) {
                p3u += en.getValue();
            }
        }
        p3 = ParkingUtil.southCountMap.get("小车区")-p3u;
        Map<String,Integer> map4 = ParkingUtil.southMap.get("后广场货车区");
        if(map4 != null) {
            for (Map.Entry<String, Integer> en : map4.entrySet()) {
                p4u += en.getValue();
            }
        }
        p4 = ParkingUtil.southCountMap.get("后广场货车区")-p4u;
        //推小屏
        int pbig = p1+p2+p4;
        if(ParkingUtil.ssStatus){
            try {
                String aa="<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.50\" Port=\"9999\"><PROG id=\"-1\"><ITEMS trad=\"0,15\" wh=\"160,128\"><ITEM type=\"2\" interval=\"65534\"><text color=\"3\" size=\"32\" style=\"0\"  font=\"宋体\">车位剩余 大车位:"+addZero(pbig)+"小车位:"+addZero(p3)+"</text>" +
                        "</ITEM></ITEMS></PROG></ITS></QDELITS>";
                ParkingUtil.pw.write(aa);
                ParkingUtil.pw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //推大屏
        if(ParkingUtil.sbStatus){
            NovaTraffic novaTraffic = new NovaTraffic("192.168.1.51", 5000);
            String content = "[all]\r\n" +
                    "items=1\r\n" +
                    "[item1]\r\n" +
                    "param=4294960,1,1,1,0,5,1,0,1\r\n" +
                    "img1=0,0,parking.png,0,0,0\r\n" +
                    "imgparam1=4294960\r\n" +
                    "time1=820,0,200,84,4294960,1,123a91,2424,2,0,1,0,1,1,1,1,1,1,0,0,1,Asia/Shanghai,GMT+08:00,星期日|星期一|星期二|星期三|星期四|星期五|星期六\r\n" +
                    //"clock1=860,10,148,148,8,4294960,Asia/Shanghai,GMT+08:00,0,5,2,1,5,3,4,2,1,1\r\n" +
                    //"txtext1=0,100,0,288,3,3232,0,0,0,1,0,1,00000000,12,0,100,1,屏幕测试，敬请谅解,0,0,0,5,5,5\r\n"+
                    "txt1=846,220,2,2828,2,123a91,0,"+ addZero(p4) +",93,28,0\r\n" +
                    "txtparam1=10,0\r\n" +
                    "txt3=846,352,2,2828,2,123a91,0,"+ addZero(p3) +",93,28,0\r\n" +
                    "txtparam3=10,0\r\n" +
                    "txt4=846,430,2,2828,2,123a91,0,"+ addZero(p2) +",93,28,0\r\n" +
                    "txtparam4=10,0\r\n" +
                    "txt5=846,525,2,2828,2,123a91,0,"+ addZero(p1) +",93,28,0\r\n" +
                    "txtparam5=10,0\r\n" +
                    "txtext1=820,85,200,24,2,2424,0,0,0,1,3,1,123a91,8,2,4294960,255,"+ParkingUtil.tempType+" "+ParkingUtil.lowTemp+"-"+ParkingUtil.highTemp+",0,0,0,5,5,5";
            int ret = novaTraffic.sendPlayList(1, content);
        }

    }

    @Override
    public List<String> pushbigS() {
        List<String> list = new ArrayList<>();
        NovaTraffic novaTraffic = new NovaTraffic("192.168.1.51", 5000);
        String name = novaTraffic.getDeviceName();
        if(name == null){
            list.add("离线");
           return list;
        }
        list.add("在线");
        Date d = new Date();
        String ds = String.valueOf(d.getTime());
        list.add("3"+ds+".jpg");
        novaTraffic.getDeviceScreenshot("d:\\NovaTraffic\\jietupic\\3"+ds+".jpg");
        return list;
    }

    @Override
    public List<String> pushbigN() {
        List<String> list = new ArrayList<>();
        NovaTraffic novaTraffic = new NovaTraffic("192.168.1.52", 5000);
        String name = novaTraffic.getDeviceName();
        if(name == null){
            list.add("离线");
            return list;
        }
        list.add("在线");
        Date d = new Date();
        String ds = String.valueOf(d.getTime());
        list.add("2"+ds+".jpg");
        novaTraffic.getDeviceScreenshot("d:\\NovaTraffic\\jietupicn\\2"+ds+".jpg");
        return list;
    }

    @Override
    public void pushsmallN(HttpServletRequest request) {
        String fontSize = request.getParameter("fontSize");
        if("60".equals(fontSize)){
            fontSize = "24";
        }else{
            fontSize = "32";
        }
        String fontColor = request.getParameter("fontColor");
        String info = request.getParameter("info");
        try {
            String aa="<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.51\" Port=\"9999\"><PROG id=\"-1\"><ITEMS trad=\"0,0\" wh=\"160,128\"><ITEM type=\"2\" interval=\"65534\"><text color=\""+fontColor+"\" size=\""+fontSize+"\" style=\"0\"  font=\"宋体\">"+info+"</text>" +
                    "</ITEM></ITEMS></PROG></ITS></QDELITS>";
            ParkingUtil.pw.write(aa);
            ParkingUtil.pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pushScreenN() {
       int p1=0,p2=0,p3=0,p4=0,p5=0;//每区剩余车位数
        int p1u = 0,p2u=0,p3u=0,p4u=0,p5u=0;//每区使用车位数
        Map<String,Integer> map1 = ParkingUtil.northMap.get("超长停车区");
        if(map1 != null) {
            for (Map.Entry<String, Integer> en : map1.entrySet()) {
                p1u += en.getValue();
            }
        }
        p1 = ParkingUtil.northCountMap.get("超长停车区")-p1u;
        Map<String,Integer> map2 = ParkingUtil.northMap.get("前广场货车区");
        if(map2 != null) {
            for (Map.Entry<String, Integer> en : map2.entrySet()) {
                p2u += en.getValue();
            }
        }
        p2 = ParkingUtil.northCountMap.get("前广场货车区")-p2u;
        Map<String,Integer> map3 = ParkingUtil.northMap.get("前广场客车区");
        if(map3 != null) {
            for (Map.Entry<String, Integer> en : map3.entrySet()) {
                p3u += en.getValue();
            }
        }
        p3 = ParkingUtil.northCountMap.get("前广场客车区")-p3u;
        Map<String,Integer> map4 = ParkingUtil.northMap.get("后广场货车区");
        if(map4 != null) {
            for (Map.Entry<String, Integer> en : map4.entrySet()) {
                p4u += en.getValue();
            }
        }
        p4 = ParkingUtil.northCountMap.get("后广场货车区")-p4u;
        Map<String,Integer> map5 = ParkingUtil.northMap.get("小车区");
        if(map5 != null) {
            for (Map.Entry<String, Integer> en : map5.entrySet()) {
                p5u += en.getValue();
            }
        }
        p5 = ParkingUtil.northCountMap.get("小车区")-p5u;
        //推小屏
        int pbig = p1+p2+p3+p4;
        if(ParkingUtil.nsStatus){
            try {
                String aa="<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.51\" Port=\"9999\"><PROG id=\"-1\"><ITEMS trad=\"0,15\" wh=\"160,128\"><ITEM type=\"2\" interval=\"65534\"><text color=\"3\" size=\"32\" style=\"0\"  font=\"宋体\">车位剩余 大车位:"+addZero(pbig)+"小车位:"+addZero(p5)+"</text>" +
                        "</ITEM></ITEMS></PROG></ITS></QDELITS>";
                ParkingUtil.pw.write(aa);
                ParkingUtil.pw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //推大屏
        if(ParkingUtil.nbStatus) {
            NovaTraffic novaTraffic = new NovaTraffic("192.168.1.52", 5000);
            String content = "[all]\r\n" +
                    "items=1\r\n" +
                    "[item1]\r\n" +
                    "param=4294960,1,1,1,0,5,1,0,1\r\n" +
                    "img1=0,0,parking.png,0,0,0\r\n" +
                    "imgparam1=4294960\r\n" +
                    "time1=820,0,200,84,4294960,1,123a91,2424,2,0,1,0,1,1,1,1,1,1,0,0,1,Asia/Shanghai,GMT+08:00,星期日|星期一|星期二|星期三|星期四|星期五|星期六\r\n" +
                    //"clock1=860,10,148,148,8,4294960,Asia/Shanghai,GMT+08:00,0,5,2,1,5,3,4,2,1,1\r\n" +
                    //"txtext1=0,100,0,288,3,3232,0,0,0,1,0,1,00000000,12,0,100,1,屏幕测试，敬请谅解,0,0,0,5,5,5\r\n"+
                    "txt1=846,200,2,2828,2,123a91,0,"+addZero(p4)+",93,28,0\r\n" +
                    "txtparam1=10,0\r\n" +
                    "txt3=846,325,2,2828,2,123a91,0,"+addZero(p5)+",93,28,0\r\n" +
                    "txtparam3=10,0\r\n" +
                    "txt4=846,400,2,2828,2,123a91,0,"+addZero(p3)+",93,28,0\r\n" +
                    "txtparam4=10,0\r\n" +
                    "txt5=846,470,2,2828,2,123a91,0,"+addZero(p2)+",93,28,0\r\n" +
                    "txtparam5=10,0\r\n" +
                    "txt6=846,540,2,2828,2,123a91,0,"+addZero(p1)+",93,28,0\r\n" +
                    "txtparam6=10,0\r\n" +
                    "txtext1=820,85,200,24,2,2424,0,0,0,1,3,1,123a91,8,2,4294960,255,"+ParkingUtil.tempType+" "+ParkingUtil.lowTemp+"-"+ParkingUtil.highTemp+",0,0,0,5,5,5";
            int ret = novaTraffic.sendPlayList(1, content);
        }
    }

    @Override
    public List<String> getParkCountS() {
        int p1=0,p2=0,p3=0,p4=0;//每区剩余车位数
        int p1u = 0,p2u=0,p3u=0,p4u=0;//每区使用车位数
        Map<String,Integer> map1 = ParkingUtil.southMap.get("超长停车区");
        if(map1 != null){
            for(Map.Entry<String,Integer> en:map1.entrySet()){
                p1u += en.getValue();
            }
        }
        p1 = ParkingUtil.southCountMap.get("超长停车区")-p1u;
        Map<String,Integer> map2 = ParkingUtil.southMap.get("前广场大车区");
        if(map2 != null) {
            for (Map.Entry<String, Integer> en : map2.entrySet()) {
                p2u += en.getValue();
            }
        }
        p2 = ParkingUtil.southCountMap.get("前广场大车区")-p2u;
        Map<String,Integer> map3 = ParkingUtil.southMap.get("小车区");
        if(map3 != null) {
            for (Map.Entry<String, Integer> en : map3.entrySet()) {
                p3u += en.getValue();
            }
        }
        p3 = ParkingUtil.southCountMap.get("小车区")-p3u;
        Map<String,Integer> map4 = ParkingUtil.southMap.get("后广场货车区");
        if(map4 != null) {
            for (Map.Entry<String, Integer> en : map4.entrySet()) {
                p4u += en.getValue();
            }
        }
        p4 = ParkingUtil.southCountMap.get("后广场货车区")-p4u;
        int pbig = p1+p2+p4;
        List<String> list = new ArrayList<>();
        list.add(addZero(pbig));
        list.add(addZero(p3));
        return list;
    }

    @Override
    public List<String> getParkCountN() {
        int p1=0,p2=0,p3=0,p4=0,p5=0;//每区剩余车位数
        int p1u = 0,p2u=0,p3u=0,p4u=0,p5u=0;//每区使用车位数
        Map<String,Integer> map1 = ParkingUtil.northMap.get("超长停车区");
        if(map1 != null) {
            for (Map.Entry<String, Integer> en : map1.entrySet()) {
                p1u += en.getValue();
            }
        }
        p1 = ParkingUtil.northCountMap.get("超长停车区")-p1u;
        Map<String,Integer> map2 = ParkingUtil.northMap.get("前广场货车区");
        if(map2 != null) {
            for (Map.Entry<String, Integer> en : map2.entrySet()) {
                p2u += en.getValue();
            }
        }
        p2 = ParkingUtil.northCountMap.get("前广场货车区")-p2u;
        Map<String,Integer> map3 = ParkingUtil.northMap.get("前广场客车区");
        if(map3 != null) {
            for (Map.Entry<String, Integer> en : map3.entrySet()) {
                p3u += en.getValue();
            }
        }
        p3 = ParkingUtil.northCountMap.get("前广场客车区")-p3u;
        Map<String,Integer> map4 = ParkingUtil.northMap.get("后广场货车区");
        if(map4 != null) {
            for (Map.Entry<String, Integer> en : map4.entrySet()) {
                p4u += en.getValue();
            }
        }
        p4 = ParkingUtil.northCountMap.get("后广场货车区")-p4u;
        Map<String,Integer> map5 = ParkingUtil.northMap.get("小车区");
        if(map5 != null) {
            for (Map.Entry<String, Integer> en : map5.entrySet()) {
                p5u += en.getValue();
            }
        }
        p5 = ParkingUtil.northCountMap.get("小车区")-p5u;
        int pbig = p1+p2+p3+p4;
        List<String> list = new ArrayList<>();
        list.add(addZero(pbig));
        list.add(addZero(p5));
        return list;
    }
    public String addZero(int val){
        if(val<0){
            val = 0;
        }
        String valS = String.valueOf(val);
        if(valS.length()==1){
            valS = "00" + valS;
        }else if(valS.length()==2){
            valS = "0" + valS;
        }
        return valS;
    }

}
